f = open("log.txt")
init_st = 30
init_fi =200
init_zero = 300
zero_term = 100

c = 0
avg = 0
avgZero = 0.0
zeroFlag = True;
zero= 0.0
preZero = 0.0
zeroCnt=0
state = False
vibState = False
for line in f:
    
    
    val = int(line)
    if c < 99999999:
        c = c + 1
    
    #
    # initialization
    #
    if c > init_st and c < init_fi :
       avg = avg + val
       continue
    elif (c == init_fi):
        avg = avg/(init_fi - init_st)
        print "Average noise is "+str(avg)
        continue
    
    # Avg zero crossing rate check
    # state of avg over is true, less is false
    if c > init_fi and c < init_zero:
        if avg < val and zeroFlag is False:
            avgZero = avgZero + 1
            zeroFlag = True
        elif avg > val and zeroFlag is True:
            avgZero =avgZero + 1
            zeroFlag = False
        else:
            continue
    elif c == init_zero:
        avgZero = avgZero/(init_zero-init_fi)
        print "Average zero crossing rate is "+str(avgZero)
        continue
    
    #
    # zero crossing check 
    #    
    if c > init_zero and c < init_zero+zero_term:
        if avg < val and zeroFlag is False:
            zero = zero + 1
            zeroFlag = True
        elif avg > val and zeroFlag is True:
            zero = zero + 1
            zeroFlag = False
        else:
            continue
    elif c == init_zero+zero_term:
        zero = zero/zero_term
        print zero
        if zero < avgZero*(3.0/5.0) and state is False:
            state = True
            print "State is Working"
        if  zero - preZero > 0 and state is True:
            vibState = True
        if vibState is True:
            print "Foot Vibration: ", str(avgZero - zero)
        if avgZero < zero+0.05 and vibState is True:
            state = False 
            vibState = False
            print "State is Not Working"
        
        c = init_zero+1
        preZero = zero
        zero = 0.0
        
   
    
    
    
    
    
        
        
         
    
             
    
    

    